package com.education.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.business.mapper.education.TopicInfoMapper;
import com.education.business.service.BaseService;
import com.education.business.service.education.TopicInfoDetailService;
import com.education.business.service.education.TopicInfoService;
import com.education.common.model.PageInfo;
import com.education.model.dto.TopicContentDTO;
import com.education.model.dto.TopicDTO;
import com.education.model.entity.StudentInfo;
import com.education.model.entity.SystemAdmin;
import com.education.model.entity.TopicInfoDetailEntity;
import com.education.model.entity.TopicInfoEntity;
import com.education.model.request.PageParam;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HP
 * @description 针对表【topic_info】的数据库操作Service实现
 * @createDate 2025-05-29 14:08:20
 */
@Service
public class TopicInfoServiceImpl extends BaseService<TopicInfoMapper, TopicInfoEntity>
        implements TopicInfoService {

    @Resource
    private TopicInfoDetailService topicInfoDetailService;

    @Override
    public PageInfo<TopicContentDTO> selectPageList(PageParam pageParam, String courseName) {
        Page<TopicContentDTO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        return selectPage(baseMapper.list(page, courseName, null, null));
    }


    @Override
    public PageInfo<TopicContentDTO> selectContentList(PageParam pageParam, Integer topicId, Integer courseId) {
        Page<TopicContentDTO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        // 主题
        PageInfo<TopicContentDTO> topicContentDtoPage = selectPage(baseMapper.list(page, null, courseId, null));
        List<TopicContentDTO> topDtoList = topicContentDtoPage.getDataList();
        if (Collections.isEmpty(topDtoList)) {
            return topicContentDtoPage;
        }
        // 回复节点
        List<Integer> topicIdList = topDtoList.stream().map(TopicContentDTO::getId).collect(Collectors.toList());
        if (Collections.isEmpty(topicIdList)) {
            return topicContentDtoPage;
        }
        List<TopicContentDTO> subTopicList = baseMapper.listDetail(topicIdList);
        Map<Integer, List<TopicContentDTO>> subTopicMap = subTopicList.stream().collect(Collectors.groupingBy(TopicContentDTO::getTopicId));
        // 组装
        topicContentDtoPage.getDataList().forEach(data -> {
            List<TopicContentDTO> subList = subTopicMap.get(data.getId());
            data.setSubTopicList(subList);
        });
        return topicContentDtoPage;
    }

    @Override
    public void createTopic(TopicDTO dto) {
        this.save(
                TopicInfoEntity.builder().courseId(dto.getCourseId())
                        .userId(dto.getUserId()).roleType(dto.getRoleType())
                        .content(dto.getContent())
                        .status(1)
                        .build()
        );
    }

    @Override
    public void replyTopic(TopicDTO dto) {
        Integer topicId = dto.getTopicId();
        Integer parentId = dto.getTopicId();
        String toUserName;
        if (!dto.getReplyType().equals(1)) {
            parentId = dto.getParentId();
            toUserName = baseMapper.getUserNameByParentId(parentId);
        } else {
            toUserName = baseMapper.getUserNameByTopicId(topicId);
        }

        topicInfoDetailService.save(
                TopicInfoDetailEntity.builder().topicId(topicId)
                        .parentId(parentId)
                        .parentType(dto.getReplyType())
                        .userId(dto.getUserId())
                        .roleType(dto.getRoleType())
                        .content(dto.getContent())
                        .status(1)
                        .toUserName(toUserName)
                        .build()
        );
    }

}




